package hu.ikti.oamadmin.types.entities;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name = "CAPABILITY")
@SequenceGenerator(sequenceName = "CAPABILITY_SEQ", name = "SEQ", allocationSize = 1)
public class Capability {

	private Long id;
	private String packageName;
	private Version version;
	private List<BundleData> bundleDatas = new ArrayList<BundleData>();

	@Id
	@Column(name = "ID")
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ")
	public Long getId() {
		return id;
	}

	@Column(name = "PACKAGE", nullable = false, length = 100)
	public String getPackageName() {
		return packageName;
	}

	@OneToOne(cascade = CascadeType.ALL)
	@JoinColumn(name = "VERSION_FK", nullable = false)
	public Version getVersion() {
		return version;
	}

	@ManyToMany(mappedBy = "capabilities")
	public List<BundleData> getBundleDatas() {
		return bundleDatas;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}

	public void setVersion(Version version) {
		this.version = version;
	}

	public void setBundleDatas(List<BundleData> bundleDatas) {
		this.bundleDatas = bundleDatas;
	}

	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((packageName == null) ? 0 : packageName.hashCode());
		result = prime * result + ((version == null) ? 0 : version.hashCode());
		return result;
	}

	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Capability other = (Capability) obj;
		if (packageName == null) {
			if (other.packageName != null)
				return false;
		} else if (!packageName.equals(other.packageName))
			return false;
		if (version == null) {
			if (other.version != null)
				return false;
		} else if (!version.equals(other.version))
			return false;
		return true;
	}

}
